package com.activiti6.demo.webservicedelegatetask;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.JavaDelegate;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class WebServiceDelegate implements JavaDelegate {
    /*
     *  这些变量我们都在流程中进行了定义，
     * 也就是说通过流程注入到了这个代理类中，当然要用activiti流程注入，
     * 就要使用activiti的数据类型Expression
     * */
    private Expression wsdl;
    private Expression operation;
    private Expression name;

    // 要注入当然要有set方法
    public void setWsdl(Expression wsdl) {
        this.wsdl = wsdl;
    }

    public void setOperation(Expression operation) {
        this.operation = operation;
    }

    public void setName(Expression name) {
        this.name = name;
    }

    @Override
    public void execute(DelegateExecution execution) { // throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        // 使用wsdl数据创建Client
        Client client = dcf.createClient((String) wsdl.getValue(execution));
        //创建请求的参数
        Object[] vars = new Object[]{name.getValue(execution)};
        //发出请求
        Object[] results = null;
        try {
            results = client.invoke((String) operation.getValue(execution), vars);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        String result = (String) results[0];
        System.out.println("↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓");
        System.out.println();
        System.out.println();
        System.out.println(vars[0] + "目前的工作地点是—————————————————————————>" + result);
        System.out.println();
        System.out.println();
        System.out.println("↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑");
    }

}
